﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;

namespace HY.WEB.SYS
{
    public partial class _leavetypeset : PageBase
    {
        public string whe = "", stype = "";
        public string us;
        public string tab = "sys_leave";
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            if (!IsPostBack)
            {
                btnSearch.OnClientClick = Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
                fill();
                BindGrid();
            }
        }

        private void fill()
        {
            DataTable d = BLL.BLLfun.get_search(tab);
            for (int i = 0; i < d.Rows.Count; i++)
            {
                ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
            }
            ddlfiled.DataBind();

            DataTable e = BLL.BLLfun.get_where();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
            }
            ddlwhere.DataBind();


        }

        //关闭查询窗口
        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
            string key = txt.Text;
            //  stype = hidstype.Text;
            try
            {
                if (key == "") whe = BLL.pub.pubwhere.get_where_sql(Session["ID"].ToString(),tab);
                BindGrid();

            }
            catch (Exception ex)
            {
                Alert.Show("查询条件错误");
            }
            finally
            {
                whe = "";
            }
        }

        // <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            txt.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;
            string ddlw = ddlwhere.SelectedValue;
            string key = txt.Text;
            if (ddlw.Equals("="))
                whe = "" + ddlf + " like  '%" + key + "%'";
            else
            {
                if (ddlf == "sid" || ddlf == "编号")
                    whe = "" + ddlf + " " + ddlw + " " + key + "";
                else
                    whe = "" + ddlf + " " + ddlw + " '" + key + "'";

            }
            if (key == "") whe = "";

            BindGrid();
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            txt.Text = String.Empty;
            txt.ShowTrigger1 = false;
            BindGrid();
        }

        //删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {


            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row][0].ToString());
                sb.Append(",");

            }

            Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            HY.BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
            BindGrid();


        }

        private void BindGrid()
        {
            DataTable  table;

            string sqltxt = "SELECT * FROM "+tab+"";

            table = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
            Grid1.DataSource = table;
            Grid1.DataBind();
                     

        }

      

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }



        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.oa.bookmarks.get_list(us);
            //DataTable table = BLL.oa.ad.get_list(stype);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

     

     


        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "edit") //修改 
            {
                string DD = Grid1.DataKeys[e.RowIndex][0].ToString();
                DataTable dt1 = BLL.Bllbase.get_table(tab, "sid=" + DD + "");
           
                hidsid.Text = dt1.Rows[0]["sid"].ToString().Trim();
                txt1.Text = dt1.Rows[0]["lbh"].ToString().Trim();
                txt2.Text = dt1.Rows[0]["ltxt"].ToString().Trim();
             //  ddltype.SelectedIndex = ddltype.Items.IndexOf(ddltype.Items.FindByValue(dt1.Rows[0]["salsetype"].ToString()));
       

             }
            else if (e.CommandName == "Del")//删除 
            {

            } 

        }

  
        protected void btsave_Click(object sender, EventArgs e)
        {
            string user=us;
            string t1 = txt1.Text.Trim();
            string t2 = txt2.Text.Trim();

            if (t1 == "" || t2 == "") return;

         
            string hid = hidsid.Text;
            string sqltxt="";
            if (hid == "")
                sqltxt = "insert into " + tab + " values('" + t1 + "','" + t2 + "')";
            else
                sqltxt = "update " + tab + "  set lbh='" + t1 + "',ltxt='" + t2 + "' where sid="+hid;

            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
            hidsid.Text = "";

            txt1.Text="";
            txt2.Text = "";

            BindGrid();
        }

    
    }
}
